<?php
namespace Face\Controller;
class UserController extends CommonController {

    /*自动执行*/
    public function _initialize()
    {
        $user = M('wechatuser')->where(['openid'=>$_SESSION['wechatopenid']])->find();
        if (time() - $user['lastlogin'] > 12*60*60 ) {  // 判断时间是否大于24小时
            $data = [
                'qiandao'=>0,
            ];
            M('wechatuser')->where(['openid'=>$_SESSION['wechatopenid']])->save($data);
        }
    }

    /*个人中心首页*/
    public function index()
    {
        $openid = $_SESSION['wechatopenid'];
        $mmy = M('wechatuser')->where(['openid'=>$openid])->find();
        $this->code($mmy['yaoqingma']);
        $this->assign('mes',$mmy);

        /*获取access_token*/
        $op = M('wechat')->find(1);
        $appid= $op['appid'];
        $appsecret= $op['appsecret'];
        $urll = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$appsecret;

        $chh=curl_init();
        curl_setopt($chh,CURLOPT_URL,$urll);
        curl_setopt($chh, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($chh, CURLOPT_SSL_VERIFYHOST, 1);
        curl_setopt($chh,CURLOPT_RETURNTRANSFER,1);
        $date=curl_exec($chh);
        curl_close($chh);
        $jsoninfo=json_decode($date,true);
        $access_token=$jsoninfo['access_token'];
        //永久二维码
        $qrcode = '{"action_name": "QR_LIMIT_SCENE", "action_info": {"scene": {"scene_id": 123}}}';

        $url = 'https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token='.$access_token;

        $curl = curl_init();
        curl_setopt($curl,CURLOPT_URL,$url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1);
        curl_setopt($curl,CURLOPT_POST,1);
        curl_setopt($curl,CURLOPT_POSTFIELDS,$qrcode);

        curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
        $output=curl_exec($curl);
        curl_close($curl);

        $jsonin=json_decode($output,true);
        //var_dump($jsonin);
        //die();
        $ticket  = $jsonin['ticket'];
        $ticketuc = urlencode($ticket);

        $erwei = 'https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket='.$ticketuc;

        $this->assign('erwei',$erwei);
        $this->display();
    }
    /*个人二维码*/
    public function code($aa = '')
    {
        vendor("Phpqrcode.phpqrcode");
        $data = 'http://www.amijie.top/index.php?g=Wechat&m=Wechatuser&a=openi&num='.$aa;
        // 纠错级别：L、M、Q、H
        $level = 'L';
        // 点的大小：1到10,用于手机端4就可以了
        $size = 4;
        $path = './public/two_code/'.$aa.'.png';

        \QRcode::png($data, $path, $level, $size) ;
    }
    /*curl获取页面信息*/
    public function https_post($url,$data=null)
    {
        $curl = curl_init();
        curl_setopt($curl,CURLOPT_URL,$url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        if(!empty($data)){
            curl_setopt($curl,CURLOPT_POST,1);
            curl_setopt($curl,CURLOPT_POSTFIELDS,$data);
        }
        curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
        $output=curl_exec($curl);
        curl_close($curl);
        return $output;
    }
    /*收货地址主页*/
    public function adds()
    {
        $this->assign('data',M('add')->where(['openid'=>$_SESSION['wechatopenid']])->select());
        $this->display();
    }
    /*删除收货地址*/
    public function addel()
    {
        $id = i('get.id');
        if(M('add')->where(['id'=>$id])->delete()){
            $this->redirect('Face/User/adds');
        }else{
            $this->error('请稍后重试~');
        }
    }
    /*设置默认地址*/
    public function addelt()
    {
        $id = i('get.id');
        $data = [
            'add_moren'=>1,
        ];
        $where = [
            'add_moren'=>1,
            'openid'=>$_SESSION['wechatopenid'],
        ];
        $data1 = [
            'add_moren'=>0,
        ];
        M('add')->where($where)->save($data1);
        if(M('add')->where(['id'=>$id])->save($data)){
            $this->redirect('Face/User/adds');
        }else{
            $this->error('请稍后重试~');
        }
    }
    /*修改地址*/
    public function addet()
    {
        $id = i('get.id');
        $this->assign('vo',M('add')->where(['id'=>$id])->find());
        $post = i('post.');
        if(IS_POST){
            /*
            echo '<pre/>';
            var_dump($post);
            die();
            */
            if(trim(empty($post['name']))){
                $this->redirect('Face/User/feifa');
            }
            if(trim(empty($post['tel']))){
                $this->redirect('Face/User/feifa');
            }
            if(trim(empty($post['city']))){
                $this->redirect('Face/User/feifa');
            }
            if(trim(empty($post['xiangxidizhi']))){
                $this->redirect('Face/User/feifa');
            }
            if(strlen($post['tel'])!=11){
                $this->redirect('Face/User/feifa');
            }
            $data = [
                'add_name'=>$post['name'],
                'add_tel'=>$post['tel'],
                'add_city'=>$post['city'],
                'add_xiangxi'=>$post['xiangxidizhi'],
                'openid'=>$_SESSION['wechatopenid'],
            ];
            if(M('add')->where(['id'=>$id])->save($data)){
                $this->redirect('Face/User/adds');
            }else{
                $this->error('请稍后重试~');
            }
        }else {
            $this->display();
        }
    }
    /*添加收货地址*/
    public function addrs()
    {
        $post = i('post.');
        if(IS_POST){
            /*
            echo '<pre/>';
            var_dump($post);
            die();
            */
            if(trim(empty($post['name']))){
                $this->redirect('Face/User/feifa');
            }
            if(trim(empty($post['tel']))){
                $this->redirect('Face/User/feifa');
            }
            if(trim(empty($post['city']))){
                $this->redirect('Face/User/feifa');
            }
            if(trim(empty($post['xiangxidizhi']))){
                $this->redirect('Face/User/feifa');
            }
            if(strlen($post['tel'])!=11){
                $this->redirect('Face/User/feifa');
            }
            $data = [
                'add_name'=>$post['name'],
                'add_tel'=>$post['tel'],
                'add_city'=>$post['city'],
                'add_xiangxi'=>$post['xiangxidizhi'],
                'openid'=>$_SESSION['wechatopenid'],
            ];
            if(M('add')->add($data)){
                $this->redirect('Face/User/adds');
            }else{
                $this->error('请稍后重试~');
            }
        }else {
            $this->display();
        }
    }
    /*防止非法*/
    public function kong()
    {
        $this->display();
    }
    /*非法提交*/
    public function feifa()
    {
        $this->display();
    }
    /*重复*/

    public function chongfu()
    {
        $this->display();
    }
    public function kongkong()
    {
        $this->display();
    }
    /*团队2*/
    public function hell()
    {
        $my = M('wechatuser')->where(['openid'=>$_SESSION['wechatopenid']])->find();

        $id = $my['id'];

        $data = M('wechatuser')->where(['did'=>$id])->select();

        $this->assign('data',$data);

        $this->display();
    }
    /*我的团队*/
    public function hello()
    {

        $num = i('get.num');
        if($num==''){
            $this->redirect('Face/User/hell');
        }
        if(IS_GET){
            $shifu = M('wechatuser')->where(['yaoqingma'=>$num])->find();

            $id = $shifu['id'];

            /*数据库读取金额*/
            $mm = M('fankui')->find(1);

            $my  = M('wechatuser')->where(['openid'=>$_SESSION['wechatopenid']])->find();
            $money = $my['money'];
            $money2 = $money+$mm['yaoqing'];
            $jifen = $my['jifen'];
            $jifen2 = $jifen+$mm['jifen'];
            $human = $my['human'];
            $human = $human+1;

            $data = [
                'did'=>$id,
                'money'=>$money2,
                'jifen'=>$jifen2,
                'human'=>$human,
            ];

            $shifumoney = $shifu['money'];
            $shifumoney2 = $shifumoney+$mm['shangjia'];
            $shifujifen = $shifu['jifen'];
            $shifujifen2 = $shifujifen+$mm['jifen'];

            $data2 = [
                'money'=>$shifumoney2,
                'jifen'=>$shifujifen2,
            ];
            /*下级邀请人*/
            $data4 = [
                'openid'=>$_SESSION['wechatopenid'],
                'text'=>'邀请佣金 + ￥：'.$mm['yuedu'],
                'time'=>time(),
            ];
            /*上级的数据*/
            $shangjiopenid = M('wechatuser')->where(['yaoqingma'=>$num])->find();
            /*上级被邀请人*/
            $data3 = [
                'openid'=>$shangjiopenid['openid'],
                'text'=>'邀请佣金 + ￥：'.$mm['yuedu'],
                'time'=>time(),
            ];

            if($shifu['did']==$my['id']){
                $this->redirect('Face/User/chongf');
            }

            if($my['did']==$shifu['id']){
                $this->redirect('Face/User/chongfu');
            }

            if($num==$shifu['yaoqingma']){
                $this->redirect('Face/User/chongf');
            }

            if(M('wechatuser')->where(['openid'=>$_SESSION['wechatopenid']])->save($data)){
                /*添加上级反馈记录*/
                M('fankuijilu')->add($data3);
                /*添加下级反馈记录*/
                M('fankuijilu')->add($data4);

                M('wechatuser')->where(['yaoqingma'=>$num])->save($data2);

                $this->redirect('Face/User/chenggong');
            }else{
                $this->error('未知错误');
            }

        }else{
            $my = M('wechatuser')->where(['openid'=>$_SESSION['wechatopenid']])->find();

            $id = $my['id'];

            $data = M('wechatuser')->where(['did'=>$id])->select();

            $this->assign('data',$data);

            $this->display();
        }

    }
    /*只可以一次下级*/
    public function chongf()
    {
        $this->display();
    }
    /*成功*/
    public function chenggong()
    {
        $this->display();
    }
    /*有误*/
    public function er()
    {
        $this->display();
    }
    /*输入错误*/
    public function err()
    {
        $this->display();
    }
    /*提现*/
    public function tixian()
    {
        $this->assign('money',M('wechatuser')->where(['openid'=>$_SESSION['wechatopenid']])->find());
        $post = i('post.');
        $my = M('wechatuser')->where(['openid'=>$_SESSION['wechatopenid']])->find();
        if(IS_POST){
            if(trim(empty($post['name']))){
                $this->redirect('Face/User/er');
            }
            if(trim(empty($post['money']))){
                $this->redirect('Face/User/er');
            }
            if(strlen($post['tel'])!=11){
                $this->redirect('Face/User/er');
            }
            if($my['money']<1){
                $this->redirect('Face/User/err');
            }
            $data = [
                'openid'=>$_SESSION['wechatopenid'],
                'name'=>$post['name'],
                'tel'=>$post['tel'],
                'money'=>$post['money'],
                'time'=>time(),
            ];
            $nowmoney = $my['money']-$post['money'];
            $data2 = [
                'money'=>$nowmoney,
            ];
            if(M('tixian')->add($data)){
                M('wechatuser')->where(['openid'=>$_SESSION['wechatopenid']])->save($data2);
                $this->redirect('Face/User/chenggong');
            }else{
                $this->error('未知错误');
            }
        }else {
            $this->display();
        }
    }

    /*反馈记录*/
    public function fankuijilu()
    {
        $openid = $_SESSION['wechatopenid'];

        $count      = M('fankuijilu')->where(['openid'=>$openid])->count();// 查询满足要求的总记录数

        $Page       = new \Think\Page($count,30);// 实例化分页类 传入总记录数和每页显示的记录数(10)
        $Page->setConfig('theme',"%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%");
        $show       = $Page->show();// 分页显示输出

        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = M('fankuijilu')->where(['openid'=>$openid])->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();

        $this->assign(['page'=>$show,'data'=>$list]);

        $this->display();
    }

    /*签到*/
    public function today()
    {
        $user = M('wechatuser')->where(['openid'=>$_SESSION['wechatopenid']])->find();
        $fankui = M('fankui')->find(1);
        $money = $user['money']+$fankui['fenxiang'];
        $data = [
            'qiandao'=>1,
            'money'=>$money,
            'lastlogin'=>time(),
        ];
        M('wechatuser')->where(['openid'=>$_SESSION['wechatopenid']])->save($data);
        $data1 = [
            'openid'=>$_SESSION['wechatopenid'],
            'text'=>'您签到获得  + ￥:'.$fankui['fenxiang'],
            'time'=>time(),
        ];
        M('fankuijilu')->add($data1);
        $this->redirect('Face/User/index');
    }
    /*分享榜记录*/
    public function fenxiang()
    {
        $_SESSION['wechatopenid'] = 1;
        $this->assign('data',M('wechatuser')->order('human desc')->select());
        $this->display();
    }
}